from numpy import true_divide
from server.algorithm import *
from server.pdStocks import PdStocks

import pandas as pd
def ceshi():
    daily = PdStocks.getDaily('000001.SZ')
    KDJ(daily)
    BOLL(daily)
    MACD(daily,'close',30)
    #j上穿d
    tt = daily['j']-daily['d']
    v = tt.rolling(window=2).apply(getPoint)
    daily['cha'] = v
    print(daily.tail(20))

    i=0
    g = 0
    
    for index, row in daily.iterrows():
        jiange = index+7
        if row.cha>0 and len(daily)>=jiange:
            # #并且当前时间点中，靠近boll的下边线
            # if row['median']<row['close']:
            #     k = True
            # else:
            #     k = False

            #30日均线向上
            if daily.iloc[index-1]['M30']<row['M30']:
                k=True
            else:
                k=False

            if not k :
                if row.close < daily.iloc[jiange].close:
                    g+=1
                else:
                    i+=1

    print('shibai:',i)
    print('chenggong:',g)

def getPoint(x):
    if x.iloc[0]<0 and x.iloc[1]>=0:
        # print(x.index.start)
        return x.index.start
    else:
        return 0

#今日大于前面11个交易日的平均交易量的2倍，


daily = []
def ceshi2():
    global daily
    daily = PdStocks.getDaily('000011.SZ')
    MACD(daily,'close',30)

    vv = daily['vol'].rolling(window=15).apply(getPoint2)
    print(len(vv[vv>0]))
    daily['vv'] = vv
    print(daily[(daily['vv']>0) & (daily['change']>0)])
    i=0
    g = 0
    for index,row in daily.iterrows():
        jiange = index+3
        if row.vv>0 and row.change>0 and len(daily)>=jiange:
            # #并且当前时间点中，靠近boll的下边线
            # if row['median']<row['close']:
            #     k = True
            # else:
            #     k = False

            #30日均线向上
            if daily.iloc[index-1]['M30']<row['M30']:
                k=True
            else:
                k=False

            if k :
                if row.close < daily.iloc[jiange].close:
                    g+=1
                else:
                    i+=1

    print('shibai:',i)
    print('chenggong:',g)




def getPoint2(x):
    global daily
    index = x.index.stop
    if index<len(daily):
        if daily.iloc[index]['vol']>x.mean()*2:
            return 1
        else:
            return 0
    else:
        return 0
ceshi2()